package org.glorylight.hacks.social.controller;

import java.util.List;
import java.util.UUID;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.glorylight.hacks.social.data.TipsData;
import org.glorylight.hacks.social.service.TipsService;
import org.glorylight.hacks.social.util.UUIDHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
@Path("/tips")
public class TipsController extends AbstractBaseController {
	
	private static final Logger log = LoggerFactory.getLogger(TipsController.class);
	
	@Autowired
	private TipsService tipsService;
	
	@POST
	@Path("/add/")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response addTip(@FormParam("startAge") Float startAge, @FormParam("endAge") Float endAge,
			@FormParam("tags") String tags, @FormParam("title") String title, @FormParam("tipsDetail") String tipsDetail) {
		String id = UUIDHelper.uuidToString(UUID.randomUUID());
		try {
			TipsData tipsData = new TipsData(id, startAge, endAge, tags, title, tipsDetail);
			tipsService.addTips(tipsData);
			return Response.status(Status.OK).build();
		}  catch(Exception e) {
			log.error("can not add tips to Id : {}", id, e);
			return Response.status(Status.PRECONDITION_FAILED).build();
		}
	}
	
	@POST
	@Path("/update/{id}")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response updateTip(@PathParam("id") String id, @FormParam("startAge") Float startAge, @FormParam("endAge") Float endAge,
			@FormParam("tags") String tags, @FormParam("title") String title, @FormParam("tipsDetail") String tipsDetail) {
		try {
			TipsData tipsData = new TipsData(id, startAge, endAge, tags, title, tipsDetail);
			tipsService.updateTips(tipsData);
			return Response.status(Status.OK).build();
		}  catch(Exception e) {
			log.error("can not update tips to Id : {}", id, e);
			return Response.status(Status.PRECONDITION_FAILED).build();
		}
	}
	
	@POST
	@Path("/delete/{id}")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response deleteTip(@PathParam("id") String id, @FormParam("startAge") Float startAge, @FormParam("endAge") Float endAge,
			@FormParam("tags") String tags, @FormParam("title") String title, @FormParam("tipsDetail") String tipsDetail) {
		try {
			TipsData tipsData = new TipsData(id, startAge, endAge, tags, title, tipsDetail);
			tipsService.deleteTips(tipsData);
			return Response.status(Status.OK).build();
		}  catch(Exception e) {
			log.error("can not delete tips to Id : {}", id, e);
			return Response.status(Status.PRECONDITION_FAILED).build();
		}
	}
	
	@GET
	@Path("/find/{age}")
	@Produces(MediaType.APPLICATION_JSON)
	public List<TipsData> listTipsData(@PathParam("age") Float age) {
		try {
			List<TipsData> listData = tipsService.findTipsData(age);
			return listData;
		}  catch(Exception e) {
			log.error("can not find tips for Age : {}", age, e);
			return null;
		}
	}
}
